package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallPoster;
import com.tencreat.service.mall.IMallPosterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 海报配置Controller
 *
 * @author tencreat
 * @date 2022-01-10
 */
@Api(tags="海报配置api")
@RestController
@RequestMapping("/system/poster")
public class AdminMallPosterController extends BaseController {
    @Autowired
    private IMallPosterService mallPosterService;

    /**
     * 查询海报配置列表
     */
    @ApiOperation(value="查询海报配置列表")
    @PreAuthorize("@ss.hasPermi('system:poster:list')")
    @PostMapping("/list")
    public HttpResult<List<MallPoster>> list(@RequestBody MallPoster mallPoster) {
        QueryWrapper<MallPoster> wrapper = Wrappers.query(mallPoster);
        wrapper.orderByDesc("create_time");
        Page<MallPoster> page =  new Page<>(mallPoster.getPageNum(), mallPoster.getPageSize());
        return HttpResult.ok(mallPosterService.page(page, wrapper).getRecords());
    }

    /**
     * 查询海报配置列表
     */
    @ApiOperation(value="查询海报配置列表")
    @PreAuthorize("@ss.hasPermi('system:poster:list')")
    @PostMapping("/allList")
    public HttpResult<List<MallPoster>> allList() {
        QueryWrapper<MallPoster> wrapper =new QueryWrapper<>();
        wrapper.orderByAsc("sort_order");
        return HttpResult.ok(mallPosterService.list(wrapper));
    }

    /**
     * 导出海报配置列表
     */
    @ApiOperation(value="导出海报配置列表")
    @PreAuthorize("@ss.hasPermi('system:poster:export')")
    @Log(title = "商城管理>海报配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallPoster mallPoster) {
        List<MallPoster> list = mallPosterService.selectMallPosterList(mallPoster);
        ExcelUtil<MallPoster> util = new ExcelUtil<MallPoster>(MallPoster.class);
        return util.exportExcel(list, "poster");
    }

    /**
     * 获取海报配置详细信息
     */
    @ApiOperation(value="获取海报配置详细信息")
    @PreAuthorize("@ss.hasPermi('system:poster:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallPoster> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallPosterService.selectMallPosterById(id));
    }

    /**
     * 新增海报配置
     */
    @ApiOperation(value="新增海报配置")
    @PreAuthorize("@ss.hasPermi('system:poster:add')")
    @Log(title = "商城管理>海报配置", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallPoster mallPoster) {
        // 默认是分享海报
        if (mallPoster.getType() == null){
            mallPoster.setType(1);
        }
        int count = mallPosterService.lambdaQuery().eq(MallPoster::getType,mallPoster.getType()).count();
        if(count > 2){
                return HttpResult.error("最多只能三张");
        }

        return to(mallPosterService.insertMallPoster(mallPoster));
    }

    /**
     * 新增海报配置
     */
    @ApiOperation(value="新增开机广告")
    @PreAuthorize("@ss.hasPermi('system:poster:add')")
    @Log(title = "商城管理>开机广告", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addUp")
    public HttpResult<Boolean> addUp(@RequestBody  MallPoster mallPoster) {
        // 默认是开机海报
        if (mallPoster.getType() == null){
            mallPoster.setType(2);
        }
        int count = mallPosterService.lambdaQuery().eq(MallPoster::getType,mallPoster.getType()).count();
        if(count>0){
            return HttpResult.error("最多只能一张");
        }
        return to(mallPosterService.insertMallPoster(mallPoster));
    }

    /**
     * 修改海报配置
     */
    @ApiOperation(value="修改海报配置")
    @PreAuthorize("@ss.hasPermi('system:poster:edit')")
    @Log(title = "商城管理>海报配置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallPoster mallPoster) {
        return to(mallPosterService.updateMallPoster(mallPoster));
    }

    @ApiOperation(value="修改开机广告")
    @PreAuthorize("@ss.hasPermi('system:poster:edit')")
    @Log(title = "商城管理>开机广告", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editUp")
    public HttpResult<Boolean> editUp(@RequestBody  MallPoster mallPoster) {
        return to(mallPosterService.updateMallPoster(mallPoster));
    }

    /**
     * 删除海报配置
     */
    @ApiOperation(value="删除海报配置")
    @PreAuthorize("@ss.hasPermi('system:poster:remove')")
    @Log(title = "商城管理>海报配置", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallPosterService.deleteMallPosterByIds(ids));
    }

    @ApiOperation(value="删除开机广告")
    @PreAuthorize("@ss.hasPermi('system:poster:remove')")
    @Log(title = "商城管理>开机广告", businessType = BusinessType.DELETE)
    @PostMapping("/removeUp")
    public HttpResult<Boolean> removeUp(@RequestBody  ReqIdsParam ids) {
        return to(mallPosterService.deleteMallPosterByIds(ids));
    }

    /*****
     *  将当前分类的排序减1 顺序上升一位
     * @param mallPoster
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Integer> reSortOrderReduce(@RequestBody MallPoster mallPoster){
        return HttpResult.ok(mallPosterService.reSortOrderReduce(mallPoster));
    }

    /*****
     * 将当前分类的排序加1 顺序下降一位
     * @param mallPoster
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Integer> reSortOrderPlus(@RequestBody MallPoster mallPoster){
        return HttpResult.ok(mallPosterService.reSortOrderPlus(mallPoster));
    }


}
